package com.isunland.managesystem.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.EvaluateLogAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.EvaluateLog;
import com.isunland.managesystem.entity.EvaluateLogListOriginal;
import com.isunland.managesystem.entity.ReceviceOrderLog;
import com.isunland.managesystem.utils.MyUtils;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.json.JSONException;

/**
 * 对象功能: 评价订单及评价列表
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2017/3/19 23:04
 */

public class EvaluateLogListFragment extends BaseListFragment {
  public static final String EXTRA_ITEM =
      EvaluateLogListFragment.class.getSimpleName() + ".EXTRA_ITEM";
  //public static final String EXTRA_MEMBER_CODE = EvaluateLogListFragment.class.getSimpleName() + ".EXTRA_MEMBER_CODE";
  @BindView(R.id.tv_cooperativeMemberName) TextView tvCooperativeMemberName;
  @BindView(R.id.rb_creditworthiness) RatingBar rbCreditworthiness;
  @BindView(R.id.rb_workQuality) RatingBar rbWorkQuality;
  @BindView(R.id.rb_attitude) RatingBar rbAttitude;
  @SuppressWarnings("unused") @BindView(R.id.btn_evaluate) Button btnEvaluate;
  @BindView(R.id.tv_creditworthiness) TextView tvCreditworthiness;
  @BindView(R.id.tv_workQuality) TextView tvWorkQuality;
  @BindView(R.id.tv_attitude) TextView tvAttitude;
  private ArrayList<EvaluateLog> mList;
  private EvaluateLogAdapter<EvaluateLog> mAdapter;
  /**
   * 预约单id
   */
  private String mMainId;
  /**
   * 合作组织编码
   */
  private String mOrgMemberCode;
  /**
   * 合作组织名称
   */
  private String mOrgMemberName;

  public static Fragment newInstance(ReceviceOrderLog item) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_ITEM, item);
    EvaluateLogListFragment fragment = new EvaluateLogListFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override public String getUrl() {
    return ApiConst.URL_QUERY_LIST_EVALUATE;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<>();
    params.put("orgMemberCode", mOrgMemberCode);
    return params;
  }

  @Override public void resolveResponse(String json) {
    EvaluateLogListOriginal original = new Gson().fromJson(json, EvaluateLogListOriginal.class);
    if (original == null || original.getRows() == null || original.getResult() != Base.SUCCESS) {
      ToastUtil.show("获取列表失败");
      return;
    }
    mList.clear();
    mList.addAll(original.getRows());
    mAdapter.notifyDataSetChanged();
    tvAttitude.setText(original.getRows().get(0).getAttitude());
    tvCreditworthiness.setText(original.getRows().get(0).getCreditworthiness());
    tvWorkQuality.setText(original.getRows().get(0).getWorkQuality());
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitleCustom(R.string.evaluate);
    ReceviceOrderLog item = (ReceviceOrderLog) getArguments().getSerializable(EXTRA_ITEM);
    if (item != null) {
      mMainId = item.getMainId();
      mOrgMemberCode = item.getCooperativeMemberCode();
      mOrgMemberName = item.getCooperativeMemberName();
    } else {
      mMainId = "";
      mOrgMemberCode = "";
      mOrgMemberName = "";
    }
    mList = new ArrayList<>();
    mAdapter = new EvaluateLogAdapter<>(mActivity, mList);
  }

  public void initView(View view, Bundle savedInstanceState) {
    setListAdapter(mAdapter);
    View header =
        getLayoutInflater(savedInstanceState).inflate(R.layout.header_evaluate_log, mListview,
            false);
    ButterKnife.bind(this, header);
    mListview.addHeaderView(header);
    tvCooperativeMemberName.setText(mOrgMemberName);
  }

  @OnClick({ R.id.btn_evaluate }) public void onClick(View view) {
    if (view == null) return;
    switch (view.getId()) {
      case R.id.btn_evaluate:
        evaluate();

        break;
    }
  }

  private void evaluate() {
    int attitude = (int) rbAttitude.getRating();
    int creditworthiness = (int) rbCreditworthiness.getRating();
    int workQuality = (int) rbWorkQuality.getRating();
    if (attitude == 0 || creditworthiness == 0 || workQuality == 0) {
      ToastUtil.show("请填写完整!");
      return;
    }
    volleyPostEvaluate(attitude, creditworthiness, workQuality);
  }

  /**
   * 提交评价
   */
  private void volleyPostEvaluate(int attitude, int creditworthiness, int workQuality) {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_UPDATE_EVALUATE);
    HashMap<String, String> params = new HashMap<>();
    params.put("creditworthiness", String.valueOf(creditworthiness));
    params.put("workQuality", String.valueOf(workQuality));
    params.put("attitude", String.valueOf(attitude));
    params.put("mainId", mMainId);
    params.put("id", UUID.randomUUID().toString());
    MyUtils.showDialogLoading(mActivity, R.string.loadingSubmit);
    mActivity.volleyPost(url, params, new VolleyResponse() {
      @Override public void onVolleyError(VolleyError arg0) {
        ToastUtil.show(R.string.failure_operation);
      }

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        Base base = new Gson().fromJson(arg0, Base.class);
        ToastUtil.show(base.getMessage());
        if (Base.SUCCESS == base.getResult()) {
          volleyPost();
        }
      }
    });
  }
}
